export const BASE_URL = 'http://crm.com/addons/qingdong/'
export const BASE_IMG_URL = 'http://crm.com/assets/addons/qingdong/mini/'

import util from './util.js'

//微信请求
export const myRequest = (url, data, method, loading=true) => {
	let loginType = ''
	if(loading){
		uni.showLoading({title:'加载中...'})
	}
	// #ifdef MP-WEIXIN
	loginType = 'wx'
	// #endif
	return new Promise((resolve, reject) => {
		uni.request({
			url: BASE_URL + url,
			method: method || 'POST',
			data: data || {},
			header: {
				'login-type': loginType,
				'Content-Type': "application/json",
				'token': uni.getStorageSync('token'),
			},
			success: (res) => {
				if(loading){
					uni.hideLoading();
				}
				res = res.data
				if (res.code == 1) {
					resolve(res)
				} else if (res.code == 401) {
					uni.navigateTo({
						url: '/pages/public/login'
					})
				} else if (res.code == 402 || res.role == 0) {
					//未完善 用户 信息
					uni.showModal({
						title: '提示',
						content: res.msg,
						showCancel: false,
						success: (res) => {
							uni.navigateTo({
								url: '/pagesA/profile/mySet/setPersonData/setPersonData'
							})
						}
					})
				} else if (res.code == 405) {
					uni.showModal({
						title: '提示',
						content: res.msg,
						showCancel: false,
						success: (res) => {
						}
					})
					uni.reLaunch({
						url: '/pages/index/examineing'
					})
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				}
			},
			fail: (err) => {
				if(loading){
					uni.hideLoading();
				}
				// #ifdef MP-ALIPAY
				err = err.data
				if(err.code == 401){
					uni.showToast({
						title: err.msg,
						icon: 'none'
					})
					setTimeout(()=>{
						uni.navigateTo({
							url: '/pages/public/login'
						})
					},2000)
				}
				// #endif
				// #ifndef MP-ALIPAY
				uni.showToast({
					title: '正在拼命加载中',
					icon: 'none'
				})
				reject(err)
				// #endif
			}
		})
	})
}
